<%@ page import="java.net.URLEncoder" %>
<%@ page import="org.bson.Document" %>
<%@ page import="com.mongodb.client.*" %>
<%@ page import="com.mongodb.BasicDBList" %>
<%@ page import="java.util.regex.Pattern" %>
<%@ page import="com.mongodb.BasicDBObject" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Mongo学习</title>
    <style>
        span {
            color: red;
            font-weight: bold;
            font-size: large;
        }
    </style>

    <script type="text/javascript">
        function deleteOne(sno) {
            if (confirm("你确定要删除学号为" + sno + "的学生吗？")) {
                window.location = "delete.jsp?sno=" + sno;
            }
        }

        function deleteMany() {
            var list = document.getElementsByClassName("snos");
            var snos = [];
            for (var i = 0; i < list.length; i++) {
                if (list[i].checked) {
                    snos.push(list[i].value);
                }
            }

            if (snos.length > 0) {
                if (confirm("你确定要删除这" + snos.length + "个学生吗？")) {
                    window.location = "delete.jsp?sno=" + snos.join(",");
                }

            } else {
                alert("请选择一个学生删除")
            }

        }

        function checkAll() {
            var list = document.getElementsByClassName("snos");
            var checkAll = document.getElementById("checkAll");
            for (var i = 0; i < list.length; i++) {
                list[i].checked = checkAll.checked;
            }
        }

    </script>

</head>
<body>
<%
    int pageNum = 1;
    int pageSize = 20;
    try {
        pageNum = Integer.parseInt(request.getParameter("pageNum"));
    } catch (Exception e) {
        pageNum = 1;
    }

    MongoClient conn = MongoClients.create("mongodb://g08:" + URLEncoder.encode("abc@8") + "@mongo.zhledu.cn:27017/g08");
    MongoDatabase g08 = conn.getDatabase("g08");
    MongoCollection<Document> student = g08.getCollection("student");

    BasicDBList queryList = new BasicDBList();

    String name = request.getParameter("name");
    if (name != null && name.length() != 0) {
        queryList.add(new BasicDBObject().append("name", Pattern.compile(name)));
    } else {
        name = "";
    }
    String idCard = request.getParameter("idCard");
    if (idCard != null && idCard.length() != 0) {
        queryList.add(new BasicDBObject().append("idCard", Pattern.compile(idCard)));
    } else {
        idCard = "";
    }
    String major = request.getParameter("major");
    if (major != null && major.length() != 0) {
        queryList.add(new BasicDBObject().append("major", Pattern.compile(major)));
    } else {
        major = "";
    }
    String phone = request.getParameter("phone");
    if (phone != null && phone.length() != 0) {
        queryList.add(new BasicDBObject().append("phone", Pattern.compile(phone)));
    } else {
        phone = "";
    }
    String sex = request.getParameter("sex");
    if (sex != null && sex.length() > 0) {
        queryList.add(new BasicDBObject().append("sex", sex));
    } else {
        sex = "";
    }
    BasicDBObject query = new BasicDBObject();
    if (queryList.size() > 0) {
        query.append("$and", queryList);
    }
    long totalNum = student.countDocuments(query);
    long totlPageNum = (totalNum % pageSize == 0) ? (totalNum / pageSize) : (totalNum / pageSize) + 1;
    FindIterable<Document> list = student.find(query).sort(new Document("createTime", -1)).skip((pageNum - 1) * pageSize).limit(pageSize);

    String q = "&name=" + name + "&idCard=" + idCard + "&sex=" + sex + "&major=" + major + "&phone=" + phone;
%>
<center>
    <form action="index.jsp">
        姓名：<input type="text" name="name" value="<%=name%>">
        性别:<input type="radio" name="sex" value="" <%=sex==null||sex.length()==0?"checked":""%>>所有<input type="radio"
                                                                                                         name="sex"
                                                                                                         value="男" <%="男".equals(sex)?"checked":""%>>男
        <input type="radio" name="sex" value="女" <%="女".equals(sex)?"checked":""%>>女
        身份证:<input name="idCard" value="<%=idCard%>">
        专业：<input name="major" value="<%=major%>">
        手机号码:<input name="phone" value="<%=phone%>">
        <input type="submit" value="查询">
        <input type="reset" value="重置">
    </form>
    <a href="add.jsp">添加</a>
    <a href="javascript:deleteMany()">删除</a>
    <table border="1px" width="80%">
        <tr>
            <td>
                <input type="checkbox" name="checkAll" id="checkAll" onchange="checkAll()">全选
            </td>
            <td>学号</td>
            <td>姓名</td>
            <td>性别</td>
            <td>身份证号</td>
            <td>手机号</td>
            <td>专业</td>
            <td>操作</td>
        </tr>

        <%
            for (Document d : list) {
                out.print(" <tr>");
                out.print("<td><input class='snos' type=\"checkbox\" value='" + d.get("sno") + "'></td>");
                out.print(" <td>" + d.get("sno") + "</td>");
                out.print(" <td>" + d.get("name") + "</td>");
                out.print(" <td>" + d.get("sex") + "</td>");
                out.print(" <td>" + d.get("idCard") + "</td>");
                out.print(" <td>" + d.get("phone") + "</td>");
                out.print(" <td>" + d.get("major") + "</td>");
                out.print(" <td><a href='view.jsp?sno=" + d.get("sno") + "'>查看</a>|<a href='update.jsp?sno="+d.get("sno")+"'>修改</a>|<a href='javascript:deleteOne(\"" + d.get("sno") + "\")'>删除</a></td>");
                out.print(" </tr>");
            }
        %>
    </table>
    <a href="index.jsp?pageNum=1<%=q%>">首页</a>|
    <%
        if (pageNum > 1) {
    %>
    <a href="index.jsp?pageNum=<%=(pageNum-1)<=1?1:(pageNum-1)%><%=q%>">上一页</a>|
    <%
        }
    %>

    <%
        if (pageNum < totlPageNum) {
    %>
    <a href="index.jsp?pageNum=<%=(pageNum+1)>=totlPageNum?totlPageNum:(pageNum+1)%><%=q%>">下一页</a>|
    <%
        }
    %>
    <a href="index.jsp?pageNum=<%=totlPageNum%><%=q%>">尾页</a><br>
    查询出<span><%=totalNum%></span>条记录/当前第<span><%=pageNum%></span>页/共<span><%=totlPageNum%></span>页


</center>


</body>
</html>
